#!/usr/bin/env python
# coding=utf8

from app.model.user import User
from app.common.exception import ServiceError

class Manager():
    @staticmethod
    def add_user(params):
        login = params.get("login", None)

        if not login:
            raise ServiceError(message = "未指定用户名")

        password = params.get("password", None)
        if not password:
            raise ServiceError(message = "未指定身份密钥")

        #检查是否已经存在同名用户
        have_user = None
        try:
            have_user = User.get_by_login(login)
        except Exception as e:
            raise ServiceError(message = "数据库查询异常")

        if have_user:
            raise ServiceError(message = "注册失败，用户已存在")

        user = User()
        user.login = login
        user.password = password
        uid = user.save()

        return uid
